<template>
  <div class="roleGroup">
    <div class="top">
      <queryInput
        id="name"
        name="角色组名称"
        :value="query.name"
        @update:value="handleUpdate"
      />
      <queryInput
        id="platform"
        name="所属平台"
        type="select"
        :value="query.platform"
        :option="optionPlatform"
        @update:value="handleUpdate"
      />
      <div>
        <el-button type="primary" @click="handle_query">查询</el-button>
        <el-button @click="handle_resetting">重置</el-button>
      </div>
    </div>
    <div class="cont">
      <div class="cont_top">
        <div class="total_box">
          角色组总计：
          <span>{{ total }}</span>
        </div>
        <div class="operate">
          <el-button type="primary" @click="handle_add" v-if="route.meta.add">
            新建
          </el-button>
        </div>
      </div>
      <el-table
        v-loading="loading"
        :max-height="650"
        :data="tableData"
        row-key="bm"
        :header-cell-style="{
          backgroundColor: '#f7f8fa',
          color: '#8c8c8c',
          fontWeight: 400,
          cursor: 'pointer',
        }"
        :cell-style="{ color: '#000' }"
      >
        <template #empty>
          <el-empty :image-size="100" description="暂无数据" />
        </template>
        <el-table-column type="index" label="序号" width="100" />
        <el-table-column prop="roleName" label="角色组名称" />
        <el-table-column prop="roleCode" label="角色组编码" />
        <el-table-column prop="platformName" label="所属平台" />
        <el-table-column label="操作" align="center">
          <template #default="scope">
            <el-button
              type="primary"
              link
              @click="handle_drawer(scope.row)"
              v-if="route.meta.edit"
            >
              编辑
            </el-button>
            <el-button
              type="danger"
              link
              @click="handle_del(scope.row.id)"
              v-if="route.meta.del"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="cont_button">
        <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[15, 20, 30, 40]"
          layout="->,slot, sizes, prev, pager, next, jumper"
          :total="total"
          @change="handle_page"
        >
          <div class="total">共{{ Math.ceil(total / pageSize) }}页</div>
        </el-pagination>
      </div>
    </div>
    <div class="addBox" v-if="route.name !== '角色组权限管理'">
      <router-view></router-view>
    </div>
  </div>
</template>
    
<script setup>
import { deleteRole, getPage, sysPage } from "@/api/appPlatfor";
import { ElMessage, ElMessageBox } from "element-plus";
import { onMounted, reactive, ref, watch } from "vue";
import { useRoute, useRouter } from "vue-router";
const route = useRoute();
const router = useRouter();

const loading = ref(false);
const total = ref(0);
const pageNo = ref(1);
const pageSize = ref(15);
const query = reactive({
  name: "",
  platform: "",
});
const optionPlatform = ref([]);
const tableData = ref([]);

//更新查询数据
const handleUpdate = ({ key, value }) => {
  query[key] = value;
  if (!value) {
    get_page();
  }
};
//查询
const handle_query = () => {
  get_page();
};
//查询 - 重置
const handle_resetting = () => {
  query.name = "";
  query.platform = "";
  get_menuPage();
};

//跳转新增页
const handle_add = () => {
  router.push("/app-platfor/roleGroup/add");
};

//编辑
const handle_drawer = (data) => {
  router.push({
    path: "/app-platfor/roleGroup/edit",
    query: {
      id: data.id,
      platformId: data.platformId,
    },
  });
};

//删除
const handle_del = (id) => {
  ElMessageBox.confirm("确定删除该角色组吗？", {
    type: "error",
    distinguishCancelAndClose: true,
    confirmButtonText: "确定",
    cancelButtonText: "取消",
  })
    .then(async () => {
      const res = await deleteRole({ id: id });

      if (res.code === 200) {
        ElMessage({
          type: "success",
          message: res.msg,
        });
        get_page();
      } else {
        ElMessage({
          type: "error",
          message: res.msg,
        });
      }
    })
    .catch(() => {});
};

//分页
const handle_page = (currentPage, pageSize) => {
  get_page();
};

//获取所属平台
const get_sspt = async () => {
  const res = await sysPage();
  if (res.code === 200) {
    res.data.data.forEach((item) => {
      optionPlatform.value.push({
        value: item.platformId,
        label: item.platformName,
      });
    });
  }
};

//获取角色组
let time = ref(null);
const get_page = async () => {
  loading.value = true;
  const res = await getPage({
    page: pageNo.value,
    pageSize: pageSize.value,
    roleName: query.name,
    platformId: query.platform,
  });

  time.value = setTimeout(() => {
    loading.value = false;
  }, 5000);

  if (res.code === 200) {
    clearTimeout(time.value); // 清除定时器
    time.value = null;
    tableData.value = res.data.records;
    total.value = res.data.total;
    loading.value = false;
  }
};

watch(route, get_page, { deep: true });

onMounted(() => {
  get_page();
  get_sspt();
});
</script>
    
<style lang="scss" scoped>
.roleGroup {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  gap: 10px;
  position: relative;
  .top {
    width: 100%;
    padding: 10px;
    background: #fff;
    display: flex;
    flex-wrap: wrap;
    gap: 30px;

    > div {
      width: calc((100% - (6 - 1) * 30px) / 4);
      height: 40px;
      display: flex;
      align-items: center;
      gap: 10px;
    }
  }
  .cont {
    flex: 1;
    padding: 10px;
    background: #fff;

    .cont_top {
      width: 100%;
      height: 40px;
      display: flex;
      justify-content: space-between;
      align-items: center;

      .total_box {
        display: flex;
        > span {
          font-weight: bold;
          margin: 0 20px 0 5px;
        }
        p > span {
          margin: 0 5px;
          color: #09f;
        }
      }

      .operate {
        display: flex;
        gap: 10px;
      }
    }

    .cont_button {
      width: 100%;
      height: 40px;
      margin-top: 10px;
      display: flex;
      justify-content: end;
      align-items: center;
    }
  }

  .addBox {
    position: absolute;
    left: 0;
    top: 0;
    z-index: 999;
    background: #f2f3f5;
    width: 100%;
    height: 100%;
  }
}
.el-table {
  .table_name {
    color: #09f;
    cursor: pointer;
  }
  .el-button {
    margin: 0 8px;
  }
}

.drawer_header {
  width: 100%;
  height: 60px;
  border-bottom: 1px solid #ccc;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 15px;
  h3 {
    font-size: 20px;
    cursor: pointer;
  }
}

.drawer_cont {
  padding: 5px 15px;
  width: 100%;
  display: flex;
  flex-direction: column;
  .drawerForm {
    padding: 15px 0;
    // display: flex;
    // flex-wrap: wrap;
    // gap: 15px;
    // .el-form-item {
    //   width: calc(50% - 10px);
    //   width: 100%;
    // }
  }
}

.drawer_operate {
  display: flex;
  justify-content: end;
  gap: 10px;
}

.el-button {
  margin: 0;
}
</style>